<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\service\AdminUserService;

class Index extends BaseController
{
    public function index()
    {
        return view();
    }

    /**
     * @param AdminUserService $service
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login(AdminUserService $service)
    {
        $input = input();

        if (empty($input['username']) || empty($input['password'])) {
            return json(['code' => -1, 'msg' => '用户名或密码不能为空']);
        }
        if (empty($input['vercode'])) {
            return json(['code' => -1, 'msg' => '验证不能为空']);
        }

        $where = [];
        $where[] = ['username', '=', $input['username']];

        $info = $service->getWhereInfo($where);
        if (empty($info)) {
            return json(['code' => -1, 'msg' => '登录用户不存在']);
        }
        if ($info['status'] != 1) {
            return json(['code' => -1, 'msg' => '该用户已被禁止登录']);
        }
        if($info['password'] <> md5($input['password'] . $info['salt'])){
            return json(['code' => -1, 'msg' => '您输入的密码不正确']);
        }

        $service->setLogin($info['id']);

        $data = [
            'url' => '/admin/home/index'
        ];
        return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }
}
